package com.kust.smarthome.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.kust.smarthome.web.dto.CommodityDto;
import com.kust.smarthome.web.model.Commodity;
import com.kust.smarthome.web.model.OrderForm;
import com.kust.smarthome.web.model.User;
import com.kust.smarthome.web.service.ICommodityService;
import com.kust.smarthome.web.service.IOrderFormService;
import com.kust.smarthome.web.service.IUserService;
import com.kust.smarthome.web.vo.BuyCommodityVo;
import com.kust.smarthome.web.vo.CommodityByTypeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.security.Principal;
import java.time.LocalDateTime;
import java.util.UUID;

@RestController
@RequestMapping("/commodity")
@Api(value = "商品列表相关功能", tags = {"shopping-tag"})
public class CommodityController {

    @Resource
    private ICommodityService commodityService;

    @Resource
    private IUserService userService;

    @Resource
    private IOrderFormService orderFormService;

    @GetMapping("/list")
    @ApiOperation(value = "获取商品基本信息", notes = "用于展示商品的基本信息,使用了分页插件(一页6条数据暂定),需要客户端传入页码")
    public PageInfo<CommodityDto> list(Integer pageNum) {
        if (pageNum == null)
            pageNum = 1;
        Integer pageSize = 12;
        return commodityService.page4Dto(pageNum, pageSize);
    }

    @GetMapping("/selectByType")
    @ApiOperation("通过type查询商品信息")
    public PageInfo<CommodityDto> selectByType(CommodityByTypeVo commodityByTypeVo) {
        if (commodityByTypeVo.getPageNum() == null)
            commodityByTypeVo.setPageNum(1);

        commodityByTypeVo.setPageSize(6);

        return commodityService.select4DtoByType(commodityByTypeVo);
    }


    @GetMapping("/selectByName")
    @ApiOperation(value = "查询基本信息", notes = "用于展示查询到的商品的基本信息,使用了分页插件(一页6条数据暂定),需要客户端传入页码,以及查询信息")
    public PageInfo<CommodityDto> selectByName(String nameInfo, Integer pageNum) {

        if (pageNum == null)
            pageNum = 1;
        Integer pageSize = 6;

        return commodityService.select4Dto(nameInfo, pageNum, pageSize);

    }

    @GetMapping("/getById")
    @ApiOperation(value = "通过id获取详细信息", notes = "客户端传入commodityId,通过id返回对应信息,信息用于商品详细信息")
    public Commodity getById(Integer id) {
        return commodityService.getById(id);
    }

    @PostMapping("/buy")
    @ApiOperation(value = "通过商品购买", notes = "成功则跳转到(POST /orderForm/buy) 这个链接")
    public String buy(Principal principal, BuyCommodityVo buyCommodityVo) {
        User user = userService.getByUsername(principal.getName());

        String id = UUID.randomUUID().toString().replace("-", "");

        QueryWrapper<OrderForm> query = new QueryWrapper<>();
        query.orderByDesc("order_by").last("limit 1"); //获取最大排序数
        OrderForm o = orderFormService.getOne(query);

        OrderForm orderForm = new OrderForm()
                .setId(id)
                .setUserId(user.getId())
                .setCommodityId(buyCommodityVo.getCommodityId() + "")
                .setCommodityNumber(buyCommodityVo.getCommodityNum() + "")
                .setMoney(buyCommodityVo.getMoney())
                .setCreated(LocalDateTime.now())
                .setOrderBy(o.getOrderBy()+1)
                .setState(0);

        if (orderFormService.save(orderForm)) {
            return id;
        }

        return "";
    }

}
